//
//  YJTabBarController.m
//  YJTabBarPer
//
//  Created by houdage on 15/11/17.
//  Copyright © 2015年 YJHou. All rights reserved.
//

#import "YJTabBarController.h"
#import "UIImage+YJImage.h"
#import "YJTabBar.h"

#import "ZCMainVC.h"    //首页
#import "ZCScanVC.h"    //扫一扫
#import "ZCproblemVC.h"  //众筹
#import "ZCMeVC.h"  //我
#import "ZCHarmknowVC.h"  //危害须知

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

@interface YJTabBarController () <YJTabBarDelegate>

@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic,strong) YJTabBar* YjtabBar;

@end

@implementation YJTabBarController


- (NSMutableArray *)items{
    if (_items == nil) {
        
        _items = [NSMutableArray array];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加所有子控制器
    [self setUpAllChildViewController];
    
    // 自定义tabBar
    [self setUpTabBar];
}
- (void)setUpTabBar{
    // 自定义tabBar
    self.YjtabBar = [[YJTabBar alloc] initWithFrame:self.tabBar.frame];
    
//    self.YjtabBar = [[YJTabBar alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49,SCREEN_WIDTH, 49)];
    [self.YjtabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg"]];
    self.YjtabBar.delegate = self;
    self.YjtabBar.items = self.items;
    [self.view addSubview:self.YjtabBar];
    // 移除系统的tabBar
    [self.tabBar removeFromSuperview];
}

// *************************  此处添加所有的子控制器start  ***********************
#warning  -- 1.此处添加所有的子控制器
- (void)setUpAllChildViewController{
    // 1
    ZCMainVC *home = [[ZCMainVC alloc] init];
//    home.tabBarItem.badgeValue = @"2";
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tab_home_n"] selectedImage:[UIImage imageWithOriginalName:@"tab_home_p"] title:@"首页"];
    
    
    // 2
    ZCproblemVC *involve = [[ZCproblemVC alloc] init];
    [self setUpOneChildViewController:involve image:[UIImage imageNamed:@"tab_problem_n"] selectedImage:[UIImage imageWithOriginalName:@"tab_problem_p"] title:@"常见问题"];
    
    // 3
    ZCHarmknowVC *harm = [[ZCHarmknowVC alloc] init];
    [self setUpOneChildViewController:harm image:[UIImage imageNamed:@"tab_harm_n"] selectedImage:[UIImage imageWithOriginalName:@"tab_harm_p"] title:@"危害须知"];
    
    // 4
    ZCMeVC *mevc = [[ZCMeVC alloc] init];
    [self setUpOneChildViewController:mevc image:[UIImage imageNamed:@"tab_me_n"] selectedImage:[UIImage imageWithOriginalName:@"tab_me_p"] title:@"我的"];
}

//     2. 点击加号按钮的时候调用
- (void)tabBarDidClickPlusButton:(YJTabBar *)tabBar{
    ZCScanVC * scanvc = [[ZCScanVC alloc] init];
    UINavigationController *na_scanvc = [[UINavigationController alloc]initWithRootViewController:scanvc];
    [self presentViewController:na_scanvc animated:YES completion:nil];
    NSLog(@"模态");
}
// *************************  此处添加所有的子控制器end  ***********************

#pragma mark - 当点击tabBar上的按钮调用
- (void)tabBar:(YJTabBar *)tabBar didClickButton:(NSInteger)index{
    if (index == 0 && self.selectedIndex == index) {
        
    }
    self.selectedIndex = index;
}
- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title{
    vc.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    
    // 保存tabBarItem模型到数组
    [self.items addObject:vc.tabBarItem];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}

-(void)setHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed{
    self.YjtabBar.hidden = hidesBottomBarWhenPushed;
}

@end
